//
//  SEGActivityDetailUsersCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/3.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityDetailUsersCell.h"
@interface SEGActivityDetailUsersCell ()

/**
 标题
 */
@property (nonatomic, strong)UILabel *titleLabel;

@property (nonatomic, strong)UIView *leftLine;
@property (nonatomic, strong)UIView *rightLine;

/**
 剩余名额
 */
@property (nonatomic, strong)UILabel *countLabel;

@end

@implementation SEGActivityDetailUsersCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = kFont_13;
    titleLabel.textColor = kColor_3;
    [self.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(19);
    }];
    self.titleLabel = titleLabel;

    UIView *leftLine = [[UIView alloc] init];
    leftLine.backgroundColor = kColor_L2;
    [self.contentView addSubview:leftLine];
    [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(titleLabel.mas_left).offset(-15);
        make.centerY.equalTo(titleLabel);
        make.height.mas_equalTo(0.5);
        make.width.mas_equalTo(57.5);
    }];
    leftLine.hidden = YES;
    self.leftLine = leftLine;
    
    UIView *rightLine = [[UIView alloc] init];
    rightLine.backgroundColor = kColor_L2;
    [self.contentView addSubview:rightLine];
    [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(15);
        make.centerY.equalTo(titleLabel);
        make.height.mas_equalTo(0.5);
        make.width.mas_equalTo(57.5);
    }];
    rightLine.hidden = YES;
    self.rightLine = rightLine;
}

- (void)setUpCellWithModel:(SEGActivityDetailModel *)model isOffLine:(BOOL)isOffLine
{
    if (model.joinCount.integerValue <= 0) {
        self.titleLabel.text = @"您竟然是第 1 个！此时不参加，更待何时？";
        self.leftLine.hidden = YES;
        self.rightLine.hidden = YES;
        if (isOffLine) {
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.contentView);
                make.top.equalTo(self.contentView).offset(19);
            }];
        }else {//只有一行 居中显示
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.contentView);
                make.centerY.equalTo(self.contentView);
            }];
        }
    }else {
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.top.equalTo(self.contentView).offset(19);
        }];
        if (isOffLine) {//线下类
            self.titleLabel.text = [NSString stringWithFormat:@"已经有%@位邻居报名了此活动",model.joinCount];
        }else {
            self.titleLabel.text = [NSString stringWithFormat:@"已经有%@位邻居参加了此活动",model.joinCount];
        }
        self.leftLine.hidden = NO;
        self.rightLine.hidden = NO;
    }
    
    UIView *lastLabel;
    if (isOffLine) {//线下类 显示剩余名额
        lastLabel= self.countLabel;
    }else {
        lastLabel = self.titleLabel;
    }
    
    [self.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag >= 100) {
            [obj removeFromSuperview];//先移除之前的头像视图 不然会有重叠
        }
    }];
    
    CGFloat xpadding = 45.0;
    [model.joinList enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, obj]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")]];
        imageView.tag = 100 + idx;
        [self.contentView addSubview:imageView];
        NSUInteger row = idx / 5;
        NSUInteger index = idx % 5;
        CGFloat topPadding = row * xpadding + 19;
//        CGFloat leftPadding = index * xpadding + (LYScreenWidth() - xpadding * 4 - 30) / 2.0;
        NSUInteger rowCount = MIN(model.joinList.count, 5);
        CGFloat leftPadding = index * xpadding + (LYScreenWidth() - xpadding * (rowCount - 1) - 30) / 2.0;
        if (idx == model.joinList.count - 1) {

            [imageView mas_makeConstraints:^(MASConstraintMaker* make) {
                make.left.equalTo(self.contentView).offset(leftPadding);
                make.top.equalTo(lastLabel.mas_bottom).offset(topPadding);
                make.width.height.mas_equalTo(30).priorityHigh();
                make.bottom.equalTo(self.contentView).mas_offset(-20).priorityHigh();
            }];

            if (model.joinList.count == 9 && [model.joinCount integerValue]> 9) {
                UIImageView *more = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_more")]];
                more.layer.masksToBounds = YES;
                more.layer.cornerRadius = 15;
                more.tag = 110;
                [self.contentView addSubview:more];
                [more mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(imageView).offset(xpadding);
                    make.top.equalTo(imageView);
                    make.width.height.mas_equalTo(30).priorityHigh();
                }];
            }
        }else {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(leftPadding);
                make.top.equalTo(lastLabel.mas_bottom).offset(topPadding);
                make.width.height.mas_equalTo(30).priorityHigh();
            }];
        }
        [imageView seg_configWithRadius:2.f];
    }];
}

- (void)setPerCount:(NSString *)perCount
{
    self.countLabel.text = [NSString stringWithFormat:@"剩余名额：%@人",perCount];
}

- (UILabel *)countLabel
{
    if (!_countLabel) {
        UILabel *countLabel = [[UILabel alloc] init];
        countLabel.font = kFont_13;
        countLabel.textColor = kColor_S1;
        countLabel.text = @"剩余名额：0人";
        [self.contentView addSubview:countLabel];
        [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(13);
            make.bottom.equalTo(self.contentView).mas_offset(-20).priorityLow();
        }];
        _countLabel = countLabel;
    }
    return _countLabel;
}

@end
